package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.PrGuaranteeTableReq;
import com.jrx.anytxn.param.dto.product.PrGuaranteeTableRes;
import com.jrx.anytxn.param.entity.PrGuaranteeTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrGuaranteeTableMapper;
import com.jrx.anytxn.param.service.product.IPrGuaranteeTableService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@CacheConfig(cacheNames = "GUARANTEE")
@Service
public class PrGuaranteeTableServiceImpl implements IPrGuaranteeTableService {

    private static final Logger logger = LoggerFactory.getLogger(PrGuaranteeTableServiceImpl.class);

    @Autowired
    private ISegmentService segmentService;
    @Resource
    private ExtPrGuaranteeTableMapper extPrGuaranteeTableMapper;

    @Override
    public PrGuaranteeTableRes findByTenIdAndGuaranteeId(String tenantId, String guaranteeId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(guaranteeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrGuaranteeTable prGuaranteeTable = extPrGuaranteeTableMapper.selectByTenIdAndGuaranteeId(tenantId, guaranteeId);
        return BeanMapping.copy(prGuaranteeTable, PrGuaranteeTableRes.class);
    }

    @Override
    public TxnPage<PrGuaranteeTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnBizException {
        Page<PrGuaranteeTableRes> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<PrGuaranteeTable> prGuaranteeTables = extPrGuaranteeTableMapper.selectByTenId(tenantId,null);
        if (prGuaranteeTables.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        List<PrGuaranteeTableRes> res = BeanMapping.copyList(prGuaranteeTables, PrGuaranteeTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public PrGuaranteeTableRes addPrGuaranteeTable(PrGuaranteeTableReq prGuaranteeTableReq) throws TxnBizException {
        PrGuaranteeTable guaranteeTable = extPrGuaranteeTableMapper.selectByTenIdAndGuaranteeId(prGuaranteeTableReq.getTenantId(), prGuaranteeTableReq.getGuaranteeId());
        if (guaranteeTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnBizException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrGuaranteeTable prGuaranteeTable = BeanMapping.copy(prGuaranteeTableReq, PrGuaranteeTable.class);
        try {
            prGuaranteeTable.setId(segmentService.getId(Constant.PR_GUARANTEE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnBizException(ErrorMsgConstant.ERROR_ID);
        }
        prGuaranteeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrGuaranteeTableMapper.insertSelective(prGuaranteeTable);
        return BeanMapping.copy(prGuaranteeTable, PrGuaranteeTableRes.class);
    }

    @CacheEvict(key="'guarantee_'+#prGuaranteeTableReq.tenantId+'_'+#prGuaranteeTableReq.guaranteeId")
    @Override
    public PrGuaranteeTableRes modifyGuaranteeTable(PrGuaranteeTableReq prGuaranteeTableReq) throws TxnBizException {
        PrGuaranteeTable guaranteeTable = extPrGuaranteeTableMapper.selectByTenIdAndGuaranteeId(prGuaranteeTableReq.getTenantId(), prGuaranteeTableReq.getGuaranteeId());
        if (guaranteeTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnBizException(TxnRespCode.NO_DATA.getMsg());
        }
        PrGuaranteeTable prGuaranteeTable = BeanMapping.copy(prGuaranteeTableReq, PrGuaranteeTable.class);
        // 更新
        extPrGuaranteeTableMapper.updateByTenIdAndGuaranteeId(prGuaranteeTable);
        return BeanMapping.copy(prGuaranteeTable, PrGuaranteeTableRes.class);
    }

    @CacheEvict(key="'guarantee_'+#tenantId+'_'+#guaranteeId")
    @Override
    public Boolean removeByTenIdAndGuaranteeId(String tenantId, String guaranteeId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(guaranteeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrGuaranteeTableMapper.deleteByTenIdAndGuaranteeId(tenantId, guaranteeId);
        return i > 0;
    }

    @Override
    public List<PrGuaranteeTableRes> findAllGuaranteeByTenId(String tenantId) {
        List<PrGuaranteeTable> prProductTableList = extPrGuaranteeTableMapper.selectByTenId(tenantId,Constant.STATUS_1);
        return BeanMapping.copyList(prProductTableList, PrGuaranteeTableRes.class);
    }

    @Cacheable(key="'guarantee_'+#tenantId+'_'+#guaranteeId")
    @Override
    public PrGuaranteeTable findByGuaranteeId(String tenantId, String guaranteeId) throws TxnBizException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(guaranteeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnBizException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrGuaranteeTable prGuaranteeTable = extPrGuaranteeTableMapper.selectByTenIdAndGuaranteeId(tenantId, guaranteeId);
        return prGuaranteeTable;
    }
}
